bunny = Image.createEmpty(40,30)
bunny:clear(Color.new(255,0,255))

bell = Image.createEmpty(20,20)
bell:clear(Color.new(255,255,255))
bell = Image.load("bell.png")

bellc= {255,255,255}
belln = 1
belln2 = 2
belld = -2


bird = Image.createEmpty(40,20)
bird:clear(Color.new(0,255,0))

bg = Image.load("bg.png")

data = {} --the 0 is dummy data
datay = 100

bunnyx = 200
bunnyy = 0

bunnyup = 0 --when you press X this number becomes high
bunnyfall = 0 --countint falltime, reset after landing

scrolly = 272

birdskip = 0
birdint = 20

function make_more_data(d)
for a=1,d do
birdskip = birdskip + 1
if birdskip == birdint then --make object a bird
	table.insert(data,{bird,math.random(478-bell:width()),datay})
	birdskip = 0
else
	table.insert(data,{bell,math.random(478-bell:width()),datay})
end
--table.remove(data,1)
datay = datay + 60
end
end

make_more_data(20) --data should be size 20

function CollisionBox(x1,y1,w1,h1,x2,y2,w2,h2)
if x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 < y2 + h2 then
	return true
end
return false

end

function collision()

for a=table.getn(data),1,-1 do
	if scrolly-data[a][3]-data[a][1]:height() > 400 then
		table.remove(data,a)
		make_more_data(1)
	elseif CollisionBox(bunnyx,scrolly-bunnyy-bunny:height(),bunny:width(),bunny:height(),data[a][2],scrolly-data[a][3]-data[a][1]:height(),data[a][1]:width(),data[a][1]:height()) then
		bunnyup = 5
		table.remove(data,a)
		make_more_data(1)
		bunnyfall = 0
		--screen:clear(Color.new(255,0,0))
		--screen.flip()
		--screen.waitVblankStart()
	end
end

end


fps = 60
fpstimer = Timer.new()




while true do
fpstimer:reset()
fpstimer:start()
input = Controls.read()
if input:start() then break end
if input:cross() and bunnyy == 0 then
	bunnyup = 5
end
if bunnyup > 0 then
	bunnyy = bunnyy + bunnyup
	bunnyup = bunnyup - .1
	if bunnyup < 0 then bunnyup = 0 end
end
bunnyy = bunnyy - (3 - bunnyup)
bunnyfall = bunnyfall + (3 - bunnyup)
if bunnyy < 0 then bunnyy = 0 end

if math.abs(input:analogX()) > 30 then
	bunnyx = bunnyx + input:analogX()/15
	if bunnyx < 1 then bunnyx = 1 end
	if bunnyx > 478 - bunny:width() then bunnyx = 478 - bunny:width() end
end

collision()

if scrolly-bunnyy < 100 then
	scrolly = scrolly - (scrolly-bunnyy - 100)/10
end
if scrolly-bunnyy > 170 and scrolly ~= 272 then
	scrolly = scrolly - (scrolly-bunnyy -170)/10
	if scrolly < 272 then scrolly = 272 end
end

if bunnyfall > 500 then
	if scrolly > 500 + 272 then --speed falling up ;)
		scrolly = scrolly - 500
		bunnyy = bunnyy - 500
	end
end


screen:clear()
screen:blit(0,scrolly-272,bg,false)
screen:blit(bunnyx,scrolly-bunnyy-bunny:height(),bunny)

locs = {}
	
for a=1,table.getn(data) do
	--screen:print(0,a*8-8,data[a][2].." "..scrolly-data[a][3]-data[a][1]:height(),Color.new(255,255,255))	
if data[a][1] == bird or true then
	screen:blit(data[a][2],scrolly-data[a][3]-data[a][1]:height(),data[a][1])
else
	if scrolly-data[a][3] >= 0 and scrolly-data[a][3]-data[a][1]:height() <= 271 then
	table.insert(locs,{0,1,data[a][2],scrolly-data[a][3],0})
	table.insert(locs,{0,0,data[a][2],scrolly-data[a][3]-data[a][1]:height(),0})
	table.insert(locs,{1,0,data[a][2]+data[a][1]:width(),scrolly-data[a][3]-data[a][1]:height(),0})
	table.insert(locs,{1,0,data[a][2]+data[a][1]:width(),scrolly-data[a][3]-data[a][1]:height(),0})
	table.insert(locs,{1,1,data[a][2]+data[a][1]:width(),scrolly-data[a][3],0})
	table.insert(locs,{0,1,data[a][2],scrolly-data[a][3],0})
	end
end

end
Gu.start3d()

Gu.enable(Gu.TEXTURE_2D)
Gu.texImage(bell)
--Gu.texFunc(Gu.TFX_MODULATE, Gu.TCC_RGB)
Gu.texScale(1, 1)

bellc[belln] = bellc[belln] + belld
bellc[belln2] = bellc[belln2] + belld
if bellc[belln] < 0 then
	bellc[belln] = 0
	bellc[belln2] = 0
	belld = 2
elseif bellc[belln] > 255 then
	bellc[belln] = 255
	bellc[belln2] = 255
	belld = -2
	belln = belln2
	belln2 = belln2 + 1
	if belln2 == 4 then
		belln2 = 1
	end
end
	
--Gu.color(Color.new(bellc[1],bellc[2],bellc[3]))
--Gu.color(Color.new(255,255,255))
Gum.drawArray(Gu.TRIANGLES, Gu.TEXTURE_32BITF+Gu.VERTEX_32BITF + Gu.TRANSFORM_2D,locs)
Gu.end3d()

screen:print(0,0,fps.." fps",Color.new(255,255,255))
screen.waitVblankStart()
screen.flip()
fps = math.floor(1000/fpstimer:time())
end